package com.zhht.checkcop.demo;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.mysql.jdbc.StringUtils;
import com.zhht.checkcop.db.MysqlDb;
import com.zhht.checkcop.db.StaticDBMap;
import com.zhht.checkcop.parser.ParserMovieInfo;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class MovieInfoInitialization implements PageProcessor{
	private final static Logger log= Logger.getLogger(MovieInfoInitialization.class);
	
	private Site site = Site.me().setCycleRetryTimes(3).setSleepTime(1500).setTimeOut(5000);
	private List<String> novelListUrl;
	public MovieInfoInitialization(){}
	public MovieInfoInitialization(List<String> novelListUrl){
		this.novelListUrl=novelListUrl;
	}
	
	private Boolean bool=true;
	
	public void process(Page page) {
    	try{
    		if(bool){
    			bool=false;
    			page.addTargetRequests(novelListUrl);
    		}else{
    			
    			String allText = page.getHtml().xpath("//div[@class='txt']/allText()").get();
    			List<String> downUrl = page.getHtml().xpath("//a/text()").all();
    			List<String> infoPicUrl = page.getHtml().xpath("//div[@class='txt']///img/@src").all();
    			
    			String updateTime = page.getHtml().xpath("//div[@class='info']/span[2]/text()").get();
		    	
		    	if(!StringUtils.isNullOrEmpty(updateTime)){
		    		updateTime=updateTime.replace("发布时间：", "");
		    	}
    			
    			List<String> picList=new ArrayList<String>();
    			for(String str:infoPicUrl){
    				if(str.indexOf("http")>-1){
	    				if(downloadPicture(str)){
	    					picList.add(str);
	    				}
    				}
    			}
		    	
    			String description="";
    			String magnet="";
    			String bt="";
    			String ftp="";
    			String[] str=allText.replace("　　", "").split("◎简介");
		    	if(str!=null&&str.length>1){
		    		if(str[1].length()>3000){
		    			description=str[1].substring(0, 3000);
		    		}else{
		    			description=str[1];
		    		}
		    	}else{
		    		if(allText.length()>3000){
		    			description = allText.substring(0, 3000);
		    		}else{
		    			description = allText;
		    		}
		    	}
		    	if(downUrl!=null&&downUrl.size()>0){
		    		for(String strUrl:downUrl){
		    			if(strUrl.indexOf("magnet")>-1){
		    				magnet=strUrl;
		    			}
		    			if(strUrl.indexOf("btbo")>-1){
		    				bt=strUrl;
		    			}
		    			if(strUrl.indexOf("ftp")>-1){
		    				ftp=strUrl;
		    			}
		    		}
		    	}
		    	page.putField("description", description);
		    	page.putField("magnet", magnet);
		    	page.putField("bt", bt);
		    	page.putField("ftp", ftp);
		    	page.putField("infoUrl", page.getRequest().getUrl());
		    	page.putField("infoPicUrl", picList);
		    	page.putField("updateTime", updateTime);
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
//    	SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
		SynchronizeNewNovel(args);
//		Mail.send("时间："+sdf.format(new Date())+"  验证 sql:select count(1) from novel_info where source = 1 and thisSyn = 0 and d = 0  失败同步书籍数量:");
    }
	private static void SynchronizeNewNovel(String[] args) {
		try{
			int oldCount=0;
        	MysqlDb mysqldb = new MysqlDb();
        	mysqldb.connect();
        	
        	StaticDBMap.existNovelNameMap = mysqldb.query("select id,infoUrl from movie_info where d = 0 and classifyId != 10 and description is null",new ArrayList(),"infoUrl");
        	
        	List<Map<String, Object>> str = mysqldb.query("select count(1) count from movie_info ");
        	oldCount = Integer.parseInt(str.get(0).get("count")+"");
        	
        	
        	int thread = args!=null&&args.length>0?Integer.parseInt(args[0]):10;
        	List<String> strList1 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 1 and d = 0 and description is null");
        	List<String> strList2 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 2 and d = 0 and description is null");
        	List<String> strList3 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 3 and d = 0 and description is null");
        	List<String> strList4 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 4 and d = 0 and description is null");
        	List<String> strList5 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 5 and d = 0 and description is null");
        	List<String> strList6 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 6 and d = 0 and description is null");
        	List<String> strList7 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 7 and d = 0 and description is null");
        	List<String> strList8 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 8 and d = 0 and description is null");
        	List<String> strList9 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 9 and d = 0 and description is null");
//        	List<String> strList10 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 10 and d = 0 and description is null");
        	List<String> strList11 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 11 and d = 0 and description is null");
        	List<String> strList12 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 12 and d = 0 and description is null");
        	mysqldb.close();
        	
        	Spider.create(new MovieInfoInitialization(strList1)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfo("1")).thread(thread).run();
        	log.info("动作片已完成");
        	Spider.create(new MovieInfoInitialization(strList2)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfo("2")).thread(thread).run();
        	log.info("喜剧片已完成");
        	Spider.create(new MovieInfoInitialization(strList3)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfo("3")).thread(thread).run();
        	log.info("爱情片已完成");
        	Spider.create(new MovieInfoInitialization(strList4)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfo("4")).thread(thread).run();
        	log.info("科幻片已完成");
        	Spider.create(new MovieInfoInitialization(strList5)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfo("5")).thread(thread).run();
        	log.info("剧情片已完成");
        	Spider.create(new MovieInfoInitialization(strList6)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfo("6")).thread(thread).run();
        	log.info("悬疑片已完成");
        	Spider.create(new MovieInfoInitialization(strList7)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfo("7")).thread(thread).run();
        	log.info("战争片已完成");
        	Spider.create(new MovieInfoInitialization(strList8)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfo("8")).thread(thread).run();
        	log.info("恐怖片已完成");
        	Spider.create(new MovieInfoInitialization(strList9)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfo("9")).thread(thread).run();
        	log.info("灾难片已完成");
//        	Spider.create(new MovieInfoInitialization(strList10)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfo("10")).thread(thread).run();
//        	log.info("连续剧已完成");
        	Spider.create(new MovieInfoInitialization(strList11)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfo("11")).thread(thread).run();
        	log.info("动漫已完成");
        	Spider.create(new MovieInfoInitialization(strList12)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfo("12")).thread(thread).run();
        	log.info("综艺片已完成");
        	
        	mysqldb.connect();
        	mysqldb.execute("update movie_info set d = 1 where IFNULL(infoMagnetUrl,'') = '' AND IFNULL(infoBtUrl,'') = '' AND IFNULL(infoFtpUrl,'') = '' AND id > "+oldCount);
        	mysqldb.close();
    	}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private boolean downloadPicture(String urlList) {
        URL url = null;
        try {
            url = new URL(urlList);
            URLConnection conn = url.openConnection();
            conn.setReadTimeout(1500);
            conn.setConnectTimeout(1500);
            conn.connect();
            return true;
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        } catch (Exception e) {
        }
        return false;
    }
}
